﻿using Microsoft.AspNetCore.Http;
using Mt.Site.Application.FileUpload.Dto;
using Mt.Site.Core.Base.Utils.FileIO.Upload;
using System.Threading.Tasks;
using static Mt.Site.Core.Base.Utils.FileIO.Upload.UpfileTemplate;

namespace Mt.Site.Application
{
    public static class FileUploadExtension
    {
        #region 单文件上传（优先,可在读取文件流之前依据 Request.ContentLength校验文件大小）

        /// <summary>
        ///  单文件上传，读取表单中第一个附件（Files[0]），生成随机的文件名（或使用原文件名）保存在服务器上
        /// </summary>
        public static async Task<UpfileResult> UploadFile(
            this IFileUpload fileUpload,
            UpFileInfo fileInfo, 
            string subDirectory,
            HttpContext httpContext,
            IUploadStrategy uploadStrategy = null) 
        {
            if (fileInfo != null)
            {
                fileInfo.AdapterSavePath(subDirectory);

                return await fileUpload.FileUpByRequest(httpContext.Request, fileInfo, uploadStrategy);
            }

            return new UpfileResult { IsOk = false, ExMsg = $"Missing configuration information" };
        } 

        /// <summary>
        ///  单文件上传，读取表单中第一个附件（Files[0]），指定服务器上保存时的文件名
        /// </summary>
        public static async Task<UpfileResult> UploadFile(
            this IFileUpload fileUpload,
            UpFileInfo fileInfo, string saveName, string subDirectory,
            HttpContext httpContext, 
            IUploadStrategy uploadStrategy = null)
        {
            if (fileInfo != null)
            {
                fileInfo.AdapterSavePath(subDirectory);

                return await fileUpload.FileUpByRequest(httpContext.Request, fileInfo, saveName, uploadStrategy);
            }

            return new UpfileResult { IsOk = false, ExMsg = $"Missing configuration information" };
        }

        #endregion

        public static FileObjectConfigDto TranslateToDto(Para config)
        {
            //Logger.Debug(config.ToJsonString());
            var dto = new FileObjectConfigDto
            {
                Alias = config.Alias,                                           // 别名
                Description = config.Description == null ? null : config.Description.ToString(), // 描述
                IsPic = config.IsPic,                                           // 是否图片上传
                Title = config.Title == null ? null : config.Title.ToString(),  // 标题
                Directory = config.UpFileInfo.Directory,                        // 目录
                ExNames = string.Join("|", config.UpFileInfo.ExNames),          // 扩展名
                MaxSize = config.UpFileInfo.MaxSize,                            // 最大值
                PermissionName = config.PermissionName,
                ALterFileName = config.ALterFileName,
                FileNameLength = config.FileNameLength,
                Name = config.UpFileInfo.Name
            };

            //缩略图
            if (config.ThumbnailUpFileInfo == null)
            {
                dto.NeedThumbnail = false;
            }
            else
            {
                dto.NeedThumbnail = true;
                dto.ThumbnailName = config.ThumbnailUpFileInfo.Name;
                dto.ThumbnailDirectory = config.ThumbnailUpFileInfo.Directory;                      // 目录
                dto.ThumbnailExNames = string.Join("|", config.ThumbnailUpFileInfo.ExNames);       // 扩展名
                dto.ThumbnailMaxSize = config.ThumbnailUpFileInfo.MaxSize;                          // 最大值
                dto.ThumbnailDescription = config.ThumbnailDescription == null ? null : config.ThumbnailDescription.ToString();
            }

            if(config.ConfigForCropper == null)
            {
                dto.NeedCropper = false;
            }
            else
            {
                dto.NeedCropper = true;
                dto.ToW = config.ConfigForCropper.ToW;
                dto.ToH = config.ConfigForCropper.ToH;
            }

            return dto;
        }
    }
}
